
//加载flag
var loading = false;

function formatDate(timeStamp){
	
	var localTime = new Date(timeStamp * 1000);
	var year = localTime.getFullYear();
	var month = parseInt(localTime.getMonth()) + 1;
	var date = localTime.getDate();

	return year + '.' + month + '.' + date;
}

function formatTime(timeStamp){
	
	var localTime = new Date(timeStamp * 1000);
	var year = localTime.getFullYear();
	var month = parseInt(localTime.getMonth()) + 1;
	var date = localTime.getDate();
	var hour = localTime.getHours();
	var minute = localTime.getMinutes();
	
	return year + '.' + month + '.' + date + '  ' + checkTime(hour) + ':' + checkTime(minute);
}

function checkTime(i){
	if (i<10){
        i = "0" + i;
    }
    return i;
}

function addItems(){
	
	if (loading) return;
	
	loading = true;
	var obj = {};
	
	if($.isArray(params)){
		
		$.each(params, function(index, item){

			if($("#" + item).val()){
				obj[item] = $("#" + item).val();
			}else{
				obj[item] = '';
			}
		});
	}

	$.post(target, obj, function(dataObj){
		
		var idStr = $('.infinite-scroll.active').attr('id');
		var tabIndex = 0;
		
		if(idStr){
			var index = idStr.indexOf('_');
			tabIndex = idStr.substr(index+1);
		}

		if (dataObj.status == '1') {

			var html = joinHTML(dataObj.info);

			if(html){
				// 添加新条目
				$(container).append(html);
				$('#lastIndex').val($(container).children().length);
				
			}
			
			// 容器发生改变,如果是js滚动，需要刷新滚动
			$.refreshScroller();
		}else if(dataObj.status == '-201') {
			
			$.confirm('身份认证已失效，请重新登录', function () {
		        var loginURL = Think.U('Public/login');
		        window.location.href = loginURL;
		        return false;
		    });
		}

		if(dataObj.is_last == '1'){
			// 加载完毕，则注销无限加载事件，以防不必要的加载
			if($('.infinite-scroll.active').children('.fff') && idStr){
				$.detachInfiniteScroll($('.infinite-scroll-preloader').eq(tabIndex-1));
				$('.infinite-scroll-preloader').eq(tabIndex-1).hide();
			}else if(!idStr){
				$.detachInfiniteScroll($('.infinite-scroll'));
				// 删除加载提示符
				$('.infinite-scroll-preloader').remove();
			}
		}

		loading = false;
	});
}

$(function(){
	$(document).on("pageInit", function(e, id, page) {

		addItems();
		
		$(page).on('infinite', function() {
			// 如果正在加载，则退出
			addItems();
		});
	});

	$.init();
});
